<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\User as User1;
use app\common\library\Sms as Smslib;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;
use think\db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
		$arr=array();
		for($i=1;$i<10;$i++){
			$arr[$i]['id']=$i;
			$arr[$i]['file']="test_file".$i.'.jpg';
		} 
	
		$this->view->assign('arr', json_encode($arr,true));
        return $this->view->fetch();
    }
	public function get_file(){
		$id=input('id');
		$file="new_test_file_".$id.".jpg";
		return $file;
	}
	public function newhu($id=0){
		if($id==0){
			return  "参数错误";
		}
		$file = "hulog/".date('Ymd',time()).".txt";
		$data['id']=$id;
		$data['sta']=0;
		$data['time']=time();
		if(file_exists($file))
		{	 
			$jsdata=file_get_contents($file);
			$jsdata=json_decode($jsdata,true);
		}else{
			$jsdata=[];
		}
		$jsdata[]=$data;
		$jsdata=json_encode($jsdata); 
		$open=fopen($file,"w" );
		fwrite($open,$jsdata."\n");
		fclose($open); 
		return $this->view->fetch();
	}
	
	public function get_hujiao(){
		$file = "hulog/".date('Ymd',time()).".txt";
		$jsdata=file_get_contents($file);
		$jsdata=json_decode($jsdata,true);
		$arr=array();
		
		foreach ($jsdata as $k=>$v){
			if($v['sta']==0){
				if(in_array($v['id'],$arr)){
					$newarr[$v['id']]['time']=$newarr[$v['id']]['time']+1;
				}else{
					$arr[]=$v['id'];
					$newarr[$v['id']]['id']=$v['id'];
					$newarr[$v['id']]['time']=1;
				}
			}
		}
		if(!empty($newarr)){
			$data['sta']=1;
			$data['msg']=$newarr;
			$data['arr']=$arr;
			
			return json($data);
		} else{
			$data['sta']=0;
			$data['msg']="";
			return json($data);
		}
	}
	public function update_sta(){
		$id=input("id");
		$file = "hulog/".date('Ymd',time()).".txt";
		$jsdata=file_get_contents($file);
		$jsdata=json_decode($jsdata,true);
		foreach ($jsdata as $k=>$v){
			 
			if($v['sta']==0&&$v['id']==$id){
				 
				$jsdata[$k]['sta']=1; 
			}
		}
		$jsdata=json_encode($jsdata);
		$open=fopen($file,"w" );
		fwrite($open,$jsdata."\n");
		fclose($open); 
	}
	
	public function hujiao($id=0){
		if($id==0){
			$msg= "参数错误";
			$sta=0;		
			echo $msg; die; 		
		}else{
			$this->getcode(15803412956,$id);
			$this->getcode(15035158859,$id);
			$this->getcode(18035101204,$id);
			$this->getcode(15503635550,$id);
			//$this->getcode(13546716300,$id);
			$sta=1;		
			$msg= "呼叫成功，管理员正在赶来";
		}
		$this->view->assign('msg', $msg);
		$this->view->assign('sta', $sta);
        return $this->view->fetch();
	}
	
	public function getcode($mobile='13546716300',$id=0)
    {
		if($id==0){
			$data['sta']=0;
			$data['msg']='参数错误';
			return json_encode($data);
		}
        //$mobile = $this->request->post("tel");
		 
        $event = $this->request->request("event");
        $event = $event ? $event : 'register';
		
        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
			 
            //$this->error(__('手机号不正确'));
			$data['sta']=0;
			$data['msg']='手机号不正确';
			return json_encode($data); 
		}
        $last = Smslib::get($mobile, $event);
        if ($last && time() - $last['createtime'] < 60) {
			 
            //$this->error(__('发送频繁'));
			$data['sta']=0;
			$data['msg']='发送频繁';
			return json_encode($data); 
        }
       /* $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
        if ($ipSendTotal >= 5) {
           // $this->error(__('发送频繁'));
		   
			$data['sta']=0;
			$data['msg']='发送频繁';
			return json_encode($data);
        }
		if ($event) {
            $userinfo = User1::getByMobile($mobile);
            if (in_array($event, ['changemobile','register']) && $userinfo) {
                //被占用
                //$this->error(__('已被占用'));
				 
				$data['sta']=0;
				$data['msg']='已被占用';
				return json_encode($data);
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                //$this->error(__('未注册'));
				
			 
				$data['sta']=0;
				$data['msg']='未注册';
				return json_encode($data);
            }
        }*/
        $ret = Smslib::send($mobile, $id, $event);
        if ($ret) {
             
			$data['sta']=1;
			$data['msg']='发送成功';
			 
			return json_encode($data);
        } else {
           
			$data['sta']=0;
			$data['msg']='发送失败，请检查短信配置是否正确';
			return json($data);
        }
    }
	
	public function test(){
	    return $this->view->fetch();
	}

}
